package com.calm.clibrary.rvadapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;

/**
 * RecyclerView通用Adapter
 * Create by Calm
 * on 2019/4/10
 */
public abstract class CommRvAdapter<T> extends RecyclerView.Adapter<CommRvHolder> {
    protected LayoutInflater layoutInflater;
    protected List<T> dataList;
    protected int layoutId;
    public CommRvAdapter(Context context,List<T> list,int layoutId){
        this.layoutInflater = LayoutInflater.from(context);
        this.dataList = list;
        this.layoutId = layoutId;
    }

    @NonNull
    @Override
    public CommRvHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = layoutInflater.inflate(layoutId,viewGroup,false);
        return new CommRvHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CommRvHolder commRvHolder, int i) {
        bindData(commRvHolder,dataList.get(i),i);
    }

    @Override
    public int getItemCount() {
        if(dataList != null){
            return dataList.size();
        }
        return 0;
    }
    public abstract void bindData(CommRvHolder holder,T data,int position);
    public void setData(List<T> list){
        this.dataList = list;
        notifyDataSetChanged();
    }
    public void appendData(List<T> list){
        if(this.dataList == null){
            this.dataList = list;
        }else {
            this.dataList.addAll(list);
        }
        notifyDataSetChanged();
    }
}
